﻿using Moq;
using PostRequestFactory.Core;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using PostRequestFactory.Core.Interfaces;
using PostRequestFactory.Core.Parameters;

namespace PostRequestFactory.UnitTests
{


    /// <summary>
    ///This is a test class for PostRequestTest and is intended
    ///to contain all PostRequestTest Unit Tests
    ///</summary>
    [TestClass()]
    public class PostRequestTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for PostRequest Constructor
        ///</summary>
        [TestMethod()]
        public void PostRequestConstructorTest()
        {
            var target = new PostRequest();

            Assert.IsNotNull(target.BodyBuilder, "BodyBuilder should be initialized in the constructor.");
            Assert.IsNotNull(target.BinaryParamsContent, "BodyBuilder should be initialized in the constructor.");
            Assert.IsTrue(target.BoundaryValue.StartsWith("---------------------------"));
        }

        /// <summary>
        ///A test for AllowBodyContentOnlyAsBytes
        ///</summary>
        [TestMethod()]
        public void AllowBodyContentOnlyAsBytesTest()
        {
            var paramList =
                new List<IRequestParameter>
                    {
                        new BinaryRequestParameter("value", "xxxȘȘȘșșaaaȚȚȚbbbÎÎ")
                    };

            var request = RequestFactory.CreateRequest(paramList);

            Assert.IsTrue(request.AllowBodyContentOnlyAsBytes, "When request parameters contains at least one binary parameter which isn't encoded with base64 then AllowBodyContentOnlyAsBytes should be set to true.");
        }

        /// <summary>
        ///A test for BinaryParamsContent
        ///</summary>
        [TestMethod()]
        public void BinaryParamsContentTest()
        {
            var paramList =
              new List<IRequestParameter>
                    {
                        new BinaryRequestParameter("value", "xxxȘȘȘșșaaaȚȚȚbbbÎÎ"),
                        new BinaryRequestParameter("value2", "xxxȘȘȘșșaaaȚȚȚbbbÎÎ")
                    };

            var request = RequestFactory.CreateRequest(paramList);

            Assert.IsTrue(request.BinaryParamsContent.Count == 2, "Number of binary params content elements should be equal with number of binary params which are not encoded in base64.");
        }

        /// <summary>
        ///A test for Body
        ///</summary>
        [TestMethod()]
        public void BasicParamsBodyTest()
        {
            var paramList =
                new List<IRequestParameter>
                    {
                        new BasicRequestParameter("value", "xxxȘȘȘșșaaaȚȚȚbbbÎÎ"),
                        new BasicRequestParameter("value2", "AbcȘaBc")
                    };

            var request = RequestFactory.CreateRequest(paramList);

            Assert.AreEqual("value=xxx%c8%98%c8%98%c8%98%c8%99%c8%99aaa%c8%9a%c8%9a%c8%9abbb%c3%8e%c3%8e&value2=Abc%c8%98aBc", request.Body);
        }

        /// <summary>
        ///A test for Body
        ///</summary>
        [TestMethod()]
        public void BasicParamsBodyAsBytesTest()
        {
            var paramList =
                new List<IRequestParameter>
                    {
                        new BasicRequestParameter("value", "xxxȘȘȘșșaaaȚȚȚbbbÎÎ"),
                        new BasicRequestParameter("value2", "AbcȘaBc")
                    };

            var request = RequestFactory.CreateRequest(paramList);

            Assert.IsTrue(Encoding.UTF8.GetBytes("value=xxx%c8%98%c8%98%c8%98%c8%99%c8%99aaa%c8%9a%c8%9a%c8%9abbb%c3%8e%c3%8e&value2=Abc%c8%98aBc").SequenceEqual(request.BodyAsBytes));
        }

        /// <summary>
        ///A test for Body
        ///</summary>
        [TestMethod()]
        public void TextParamsBodyTest()
        {
            var postRequestMock = new Mock<PostRequest> { CallBase = true };
            postRequestMock.SetupGet(s => s.BoundaryValue).Returns("-----------------------------33bd9fb9-84b3-4c61-8f1e-3b4ce11df26d");

            var paramList =
                new List<IRequestParameter>
                    {
                        new TextRequestParameter("value", "xxx"),
                        new TextRequestParameter("value2", "ccc")
                    };

            var request = RequestFactory.CreateRequest(paramList, postRequestMock.Object);

            Assert.IsTrue(
@"-------------------------------33bd9fb9-84b3-4c61-8f1e-3b4ce11df26d
Content-Disposition: form-data; name=""value""

xxx
-------------------------------33bd9fb9-84b3-4c61-8f1e-3b4ce11df26d
Content-Disposition: form-data; name=""value2""

ccc
-------------------------------33bd9fb9-84b3-4c61-8f1e-3b4ce11df26d--
".Equals(request.Body));

        }

        /// <summary>
        ///A test for Body
        ///</summary>
        [TestMethod()]
        public void TextParamsBodyAsBytesTest()
        {
            var postRequestMock = new Mock<PostRequest> { CallBase = true };
            postRequestMock.SetupGet(s => s.BoundaryValue).Returns("-----------------------------33bd9fb9-84b3-4c61-8f1e-3b4ce11df26d");

            var paramList =
                new List<IRequestParameter>
                    {
                        new TextRequestParameter("value", "xxx"),
                        new TextRequestParameter("value2", "ccc")
                    };

            var request = RequestFactory.CreateRequest(paramList, postRequestMock.Object);

            Assert.IsTrue(Encoding.UTF8.GetBytes(
@"-------------------------------33bd9fb9-84b3-4c61-8f1e-3b4ce11df26d
Content-Disposition: form-data; name=""value""

xxx
-------------------------------33bd9fb9-84b3-4c61-8f1e-3b4ce11df26d
Content-Disposition: form-data; name=""value2""

ccc
-------------------------------33bd9fb9-84b3-4c61-8f1e-3b4ce11df26d--
").SequenceEqual(request.BodyAsBytes));
        }



        /// <summary>
        ///A test for Body
        ///</summary>
        [TestMethod()]
        public void BinaryParamsBodyTest()
        {
            var postRequestMock = new Mock<PostRequest> { CallBase = true };
            postRequestMock.SetupGet(s => s.BoundaryValue).Returns("-----------------------------33bd9fb9-84b3-4c61-8f1e-3b4ce11df26d");

            var paramList =
                new List<IRequestParameter>
                    {
                        new BinaryRequestParameter("value", "xxx"),
                        new BinaryRequestParameter("value2", "ccc")
                    };

            var request = RequestFactory.CreateRequest(paramList, postRequestMock.Object);

            Assert.IsTrue(
@"-------------------------------33bd9fb9-84b3-4c61-8f1e-3b4ce11df26d
Content-Disposition: form-data; name=""value""; filename=""""
Content-Type: application/octet-stream;
Content-Transfer-Encoding: binary;

xxx
-------------------------------33bd9fb9-84b3-4c61-8f1e-3b4ce11df26d
Content-Disposition: form-data; name=""value2""; filename=""""
Content-Type: application/octet-stream;
Content-Transfer-Encoding: binary;

ccc
-------------------------------33bd9fb9-84b3-4c61-8f1e-3b4ce11df26d--
".Equals(request.Body));

        }

        /// <summary>
        ///A test for Body
        ///</summary>
        [TestMethod()]
        public void BinaryParamsBodyAsBytesTest()
        {
            var postRequestMock = new Mock<PostRequest> { CallBase = true };
            postRequestMock.SetupGet(s => s.BoundaryValue).Returns("-----------------------------33bd9fb9-84b3-4c61-8f1e-3b4ce11df26d");

            var paramList =
                new List<IRequestParameter>
                    {
                        new BinaryRequestParameter("value", "xxx"),
                        new BinaryRequestParameter("value2", "ccc")
                    };

            var request = RequestFactory.CreateRequest(paramList, postRequestMock.Object);

            Assert.IsTrue(Encoding.UTF8.GetBytes(
@"-------------------------------33bd9fb9-84b3-4c61-8f1e-3b4ce11df26d
Content-Disposition: form-data; name=""value""; filename=""""
Content-Type: application/octet-stream;
Content-Transfer-Encoding: binary;

xxx
-------------------------------33bd9fb9-84b3-4c61-8f1e-3b4ce11df26d
Content-Disposition: form-data; name=""value2""; filename=""""
Content-Type: application/octet-stream;
Content-Transfer-Encoding: binary;

ccc
-------------------------------33bd9fb9-84b3-4c61-8f1e-3b4ce11df26d--
").SequenceEqual(request.BodyAsBytes));
        }



        /// <summary>
        ///A test for ContentLengthHeader
        ///</summary>
        [TestMethod()]
        public void ContentLengthHeaderTest()
        {
            var postRequestMock = new Mock<PostRequest> { CallBase = true };
            postRequestMock.SetupGet(s => s.BoundaryValue).Returns("-----------------------------33bd9fb9-84b3-4c61-8f1e-3b4ce11df26d");

            var paramList =
                new List<IRequestParameter>
                    {
                        new BinaryRequestParameter("value", "xxx"),
                        new BinaryRequestParameter("value2", "ccc")
                    };

            var request = RequestFactory.CreateRequest(paramList, postRequestMock.Object);

            Assert.AreEqual("496;", request.ContentLengthHeader);
        }
    }
}
